package JasPerl::Compiler::PageWriter;

use strict;
use warnings;

use Carp;
use File::Spec;
use File::Path;
use IO::File;

sub new {
    my ($class, $path) = @_;
    mkpath((File::Spec->splitpath($path))[1]);
    my $fh = IO::File->new($path, "w")
        or croak "$path: $!";
    bless { out => $fh, in => '' }, $class;
}

sub close {
    return $_[0]->{out}->close();
}

sub push_indent {
    my ($self, $indent) = @_;
    push @{$self->{indent}}, int($indent) ? ' ' x int($indent) : $indent;
    $self->{in} = join('', @{$self->{indent}});
}

sub pop_indent {
    my $self = shift;
    pop @{$self->{indent}};
    $self->{in} = join('', @{$self->{indent}});
}

sub print {
    my $self = shift;
    return $self->{out}->print(@_);
}

sub println {
    my $self = shift;
    return $self->print(@_, "\n");
}

sub printin {
    my $self = shift;
    return $self->print($self->{in}, @_);
}

sub printil {
    my $self = shift;
    return $self->println($self->{in}, @_);
}

1;
